﻿/*-----------------------------------------------------------------------------!
! Customer controller class                                                    !
!------------------------------------------------------------------------------!
! System   : Inventory Management System                                       !
! Client   : UK based                                                          !
! Date     : May 2011                                                          !
! Copyright: (C) KAWISOFT Inc                                                  !
! Program  : CustomerController.cs                                             !
! Function : controls all customer functions                                   !
!------------------------------------------------------------------------------!
! Notes:                                                                       !
! Original author: Md. Shaiful Islam                                           !
===============================================================================*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Model.Panjeri;
using Model.Poribar.CustomerPanjeri;
using Model.Upadan;
using Model.Sadharon;
using Model.Sohaok;
using Model.Poribar.AddressPanjeri;


namespace InventorySys.controller
{
    public class CustomerController
    {
        #region Private method
        private ICustomerKaz customerPanjeri;
        private IAddressKaz addressPanjeri;
        private Result result;
        #endregion

        public CustomerController()
        {
            customerPanjeri = Panjeri.getCustomerPanjeri();
            addressPanjeri = Panjeri.getAddressPanjeri();
        }

        #region get all customer list
        /// <summary>
        /// GetAllCustomer method
        /// </summary>
        /// <returns></returns>
        public Result GetAllCustomer()
        {
            return customerPanjeri.getList();
        }
        #endregion

        #region get customer details
        /// <summary>
        /// GetCustomerDetails according with id
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Result GetCustomerDetails(string id)
        {
            return customerPanjeri.getById(id);
        }

        #endregion

        #region add new customer details

        /// <summary>
        /// addCustomerDetails
        /// </summary>
        /// <param name="customer"></param>
        /// <returns></returns>
        public Result addCustomerDetails(Customer customer, Address address)
        {
            // save customer information
            // validate customer information
            if (customer.AccountReference.Length == 0 || customer.AccountReference == null)
            {
                result = Util.setSuccessResult(false, "Failed to add: Account reference is missing");
                return result;
            }

            if (customer.CompanyName.Length == 0 || customer.CompanyName == null)
            {
                result = Util.setSuccessResult(false, "Failed to add: Company name is missing");
                return result;
            }

            if (customer.ContactPerson.Length == 0 || customer.ContactPerson == null)
            {
                result = Util.setSuccessResult(false, "Failed to add: Contact person is missing");
                return result;
            }

            if (customer.SaleTypeId.Length == 0 || customer.SaleTypeId == null)
            {
                result = Util.setSuccessResult(false, "Failed to add: SaleType is missing");
                return result;
            }

            // validate customer information
            if (address.Country.Length == 0 || address.Country == null)
            {
                result = Util.setSuccessResult(false, "Failed to add: Country is missing");
                return result;
            }

            if (address.Description.Length == 0 || address.Description == null)
            {
                result = Util.setSuccessResult(false, "Failed to add: Address information is missing");
                return result;
            }

            if (address.Mobile.Length == 0 || address.Mobile == null)
            {
                result = Util.setSuccessResult(false, "Failed to add: Mobile information is missing");
                return result;
            }


            // save customer information
            Result customerResult = new Result();
            customerResult = customerPanjeri.add(customer);
            if (customerResult.IsSuccess == false)
            {
                result = customerResult;
                return result;
            }

            result = customerResult;
            customer = (Customer)result.ResultObj;

            // save address information
            address.PersonId = customer.Id;
            Result addressResult = new Result();
            addressResult = addressPanjeri.add(address);
            if (addressResult.IsSuccess == false)
            {
                result = Util.setSuccessResult(true, "New Customer added successfully, but failed to add " +
                "address information.");
                return result;
            }
            else
            {
                result = Util.setSuccessResult(true, "New Customer added successfully", (object)customer.Id);
            }
            return result;            
        }
        #endregion

        #region update customer details
        /// <summary>
        /// updateCustomerDetails
        /// </summary>
        /// <param name="customer"></param>
        /// <returns></returns>
        public Result updateCustomerDetails(Customer customer, Address address)
        {
            // update customer information
            result = customerPanjeri.update(customer);
            if (result.IsSuccess)
            {
                AddressController addressController = new AddressController();
                address.PersonId = customer.Id;
                if (addressController.updateAddressDetails(address).IsSuccess)
                {
                    result.Message = "Customer details updated successfully";
                }
                else
                {
                    result.Message = "Failed to update customer information. Please try later";
                }
            }
            return result;
        }

        #endregion

        #region delete customer details
        /// <summary>
        /// deleteCustomerDetails
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Result deleteCustomerDetails(string id)
        {
            return customerPanjeri.delete(id);
        }

        #endregion
    }
}
